/*
 * Copyright (c) 2010
 */
package com.mss.fileshareweb;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mss.filesharebean.Files;
import com.mss.fileshareservice.FilesService;

/**
 * @author: Tony
 */
@Controller
@RequestMapping("/file")
public class FilesController extends MultiActionController {
    
	private final Logger logger = LoggerFactory.getLogger(FilesController.class);
	private FilesService filesService;

    public void setFilesService(FilesService filesService) {
        this.filesService = filesService;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String getFiles(Model model) {
    	logger.debug("Get all the files from DB");
    	    	
    	// add the this model to the view and display
    	model.addAttribute("files", this.filesService.listAll());
    	
    	return "files";	// the name of the jsp file
    }
    
    /**
     * upload
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    	logger.debug("Upload a file");
    	//System.out.println("Upload is call");
    	
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartRequest.getFile("file");

        Files file = new Files();
        file.setFilename(multipartFile.getOriginalFilename());
        file.setNotes(ServletRequestUtils.getStringParameter(request, "notes"));
        file.setType(multipartFile.getContentType());
        file.setFile(multipartFile.getBytes());
        // the code above reads the file supply by the client 
        
        // save the file to the database
        this.filesService.save(file);

        return "redirect:/file";	// direct browser to the page files.jsp 
    }

    /**
     * download
     */
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void download(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    	
    	//System.out.println("download is call");
    	logger.debug("Download a file");
    	
    	// get the value of the parameter name id 
        int id = ServletRequestUtils.getRequiredIntParameter(request, "id");
        
        // get the correspond file from the database
        Files file = this.filesService.find(id);
        
        // response the http request with the file content
        response.setContentType(file.getType());
        response.setContentLength(file.getFile().length);
        response.setHeader("Content-Disposition","attachment; filename=\""
        						+ file.getFilename() +"\"");
        FileCopyUtils.copy(file.getFile(), response.getOutputStream());
    }

    /**
     * delete
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    	
    	//System.out.println("Delete is call");
    	logger.debug("Delete a file");
    	
    	// get the value of the parameter name id 
        int id = ServletRequestUtils.getRequiredIntParameter(request, "id");
        
        // delete the file from the database
        this.filesService.delete(id);
        
        return "redirect:/file";	// direct browser to the page files.jsp 
    }

}
